// +build js,wasm

package jsgo

import (
	"strconv"
	"strings"
	"syscall/js"
	"unsafe"
)

func MustStr(v js.Value) string {
	switch v.Type() {
	case js.TypeString:
		return v.String()
	case js.TypeUndefined, js.TypeNull:
		return ""
	case js.TypeBoolean:
		return strconv.FormatBool(v.Bool())
	case js.TypeNumber:
		return strconv.FormatFloat(v.Float(), 'G', 30, 64)
	case js.TypeSymbol:
		return "<symbol>"
	case js.TypeObject:
		return "<object>"
	case js.TypeFunction:
		return "<function>"
	default:
		panic("bad type")
	}
}

func Bytes2Str(b []byte) string {
	return *(*string)(unsafe.Pointer(&b))
}

func Str2Bytes(s string) []byte {
	x := (*[2]uintptr)(unsafe.Pointer(&s))
	h := [3]uintptr{x[0], x[1], x[1]}
	return *(*[]byte)(unsafe.Pointer(&h))
}

func strJoin(arr ...string) string {
	if len(arr) == 0 {
		return ""
	}
	return strings.Join(arr, "")
}
